﻿using databaseoper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication
{
    public partial class Products : System.Web.UI.Page
    {
        /// <summary>
        /// 页码
        /// </summary>
        public int PageIndex = 1;
        /// <summary>
        /// 每页显示的个数
        /// </summary>
        public int PageSize = 12;

        /// <summary>
        /// 整个产品列表总数
        /// </summary>
        public int PageTotal = -1;

        /// <summary>
        /// 数据库最小价格
        /// </summary>
        public int ProductsPriceMin = -1;

        /// <summary>
        /// 数据库最大价格
        /// </summary>
        public int ProductsPriceMax = -1;

        /// <summary>
        /// 根据Url地址获取价格最小值
        /// </summary>
        public int CurrentPriceMin = -1;

        /// <summary>
        /// 根据Url地址获取价格最大值
        /// </summary>
        public int CurrentPriceMax = -1;

        /// <summary>
        /// 获取数据库中最大的和最小的价格
        /// </summary>
        private void GetProductsMinAndMax()
        {
            string minSql = "select top(1) Price from Products order by Price asc;";
            int minPrice = Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.connStr, minSql));
            ProductsPriceMin = minPrice;

            string maxSql = "select top(1) Price from Products order by Price desc;";
            int maxPrice = Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.connStr, maxSql));
            ProductsPriceMax = maxPrice;
        }

        /// <summary>
        /// 设置当前页面最小和最大价格
        /// </summary>
        private void SetCurrentProductsMinAndMax()
        {
            var queryMin = Request.QueryString["min"];
            var queryMax = Request.QueryString["max"];
            int priceMin = Convert.ToInt32(Request.QueryString["min"]);
            int priceMax = Convert.ToInt32(Request.QueryString["max"]);
            if (queryMin == null)
            {
                priceMin = ProductsPriceMin;
            }
            if (queryMax == null)
            {
                priceMax = ProductsPriceMax;
            }

            CurrentPriceMin = priceMin;
            CurrentPriceMax = priceMax;

        }


        private void GetProductsTotal()
        {
            string sql = "select count(*) from Products where Price between @PriceMin and @PriceMax";
            SqlParameter[] parames = new SqlParameter[] {
                new SqlParameter("@PriceMin",CurrentPriceMin),
                new SqlParameter("@PriceMax",CurrentPriceMax),
            };

            var data = SQLHelper.ExecuteScalar(SQLHelper.connStr, sql, parames);

            int total = Convert.ToInt32(data);

            PageTotal = total;

        }

        private void GetProductsList()
        {
            string sql = "select top(@PageSize) *  from Products where ProductsID not in (select top (@PageSize * (@PageIndex - 1)) ProductsID from Products) and Price between @PriceMin and @PriceMax";
            SqlParameter[] parames = new SqlParameter[] {
                new SqlParameter("@PageSize",PageSize),
                new SqlParameter("@PageIndex",PageIndex),
                new SqlParameter("@PriceMin",CurrentPriceMin),
                new SqlParameter("@PriceMax",CurrentPriceMax),
            };
            DataSet data = SQLHelper.ExecuteDataset(SQLHelper.connStr, CommandType.Text, sql, parames);

            if (data != null)
            {
                ProductsList.DataSource = data.Tables[0];
                ProductsList.DataBind();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int page = Convert.ToInt32(Request.QueryString["page"]);
                if (page > 1)
                {
                    PageIndex = page;
                }
                GetProductsMinAndMax();
                SetCurrentProductsMinAndMax();

                GetProductsList();
                GetProductsTotal();

                SearchPriceMin.Text = CurrentPriceMin.ToString();
                SearchPriceMax.Text = CurrentPriceMax.ToString();
            }
        }
    }
}